<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="客户机构" prop="institutionName">
        <el-input
          v-model="queryParams.institutionName"
          placeholder="请输入客户机构"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="销售订单" prop="salesOrderId">
        <el-input
          v-model="queryParams.salesOrderId"
          placeholder="请输入销售订单"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="账款描述" prop="description">
        <el-input
          v-model="queryParams.description"
          placeholder="请输入账款描述"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['finance:receivable:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['finance:receivable:edit']"
        >修改</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['finance:receivable:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="receivableList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="销售订单" align="center" prop="salesOrderNo" >
        <template #default="scope">
          <el-link type="primary" :underline="false" >{{scope.row.salesOrderNo}}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="客户机构" align="center" prop="institutionName" />
      <el-table-column label="应收金额" align="center" prop="amount" />
      <el-table-column label="账款描述" align="center" prop="description">
        <template #default="scope">
          {{ scope.row.description == null ? "- - - - - - -" : scope.row.description }}
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" >
        <template #default="scope">
          <dict-tag :options="pay_status" :value="scope.row.status" />
        </template>
      </el-table-column>

      <el-table-column label="最后更新时间" align="center" prop="updateTime" width="180"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['finance:receivable:edit']">修改</el-button>
          <el-button link type="primary" icon="CirclePlus" @click="collection(scope.row)" v-hasPermi="['finance:receivable:remove']">收款</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改应收账款对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="receivableRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="客户机构" prop="institutionName">
          <el-input v-model="form.institutionName" placeholder="请输入客户机构" />
        </el-form-item>
        <el-form-item label="销售订单" prop="salesOrderNo">
          <el-input v-model="form.salesOrderNo" placeholder="请输入关联销售订单表" />
        </el-form-item>
        <el-form-item label="客户id" prop="customerId">
          <el-input v-model="form.customerId" placeholder="请输入客户id" />
        </el-form-item>
        <el-form-item label="应收金额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入应收金额" />
        </el-form-item>
        <el-form-item label="账款描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入账款描述" />
        </el-form-item>

      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Receivable">
import { listReceivable, getReceivable, delReceivable, addReceivable, updateReceivable } from "@/api/finance/receivable";
import {useRouter} from 'vue-router'
const { proxy } = getCurrentInstance();
const { pay_status } = proxy.useDict("pay_status");

const receivableList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const router = useRouter()

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    institutionName: null,
    salesOrderId: null,
    customerId: null,
    amount: null,
    description: null,
    status: null,
    createdTime: null,
    updatedTime: null
  },
  rules: {
    institutionName: [
      { required: true, message: "客户机构不能为空", trigger: "blur" }
    ],
    salesOrderId: [
      { required: true, message: "关联销售订单表不能为空", trigger: "blur" }
    ],
    customerId: [
      { required: true, message: "客户id不能为空", trigger: "blur" }
    ],
    amount: [
      { required: true, message: "应收金额不能为空", trigger: "blur" }
    ],
    status: [
      { required: true, message: "状态：0未支付 1已支付不能为空", trigger: "change" }
    ]

  }
});

const { queryParams, form, rules } = toRefs(data);

/** 收款按钮*/
function collection(row){
  router.push({
    path:'batchBilling',
    query:{
      id:row.id,
      salesOrderNo:row.salesOrderNo,
      institutionName:row.institutionName,
      amount:row.amount,
      status:row.status
    }
  })
}

/** 查询应收账款列表 */
function getList() {
  loading.value = true;
  listReceivable(queryParams.value).then(response => {
    receivableList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    institutionName: null,
    salesOrderId: null,
    customerId: null,
    amount: null,
    description: null,
    status: null,
    createdTime: null,
    updatedTime: null
  };
  proxy.resetForm("receivableRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加应收账款";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getReceivable(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改应收账款";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["receivableRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateReceivable(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addReceivable(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('finance/receivable/export', {
    ...queryParams.value
  }, `receivable_${new Date().getTime()}.xlsx`)
}

getList();
</script>
